package com.jiyun.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiyun.entity.Goods;
import com.jiyun.entity.GoodsType;
import com.jiyun.entity.GoodsVo;
import com.jiyun.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController{

    @Autowired
    private GoodsService goodsService;

    //修改
    @RequestMapping("/update")
    public String update(Goods goods){
        goodsService.updateGoods(goods);
        return "redirect:findAll";
    }
    //去修改
    @RequestMapping("/toupdate")
    public String toupdate(Integer id,Model model){
        //根据主键查询讯息
        Goods goods = goodsService.findGoodsById(id);
        model.addAttribute("goods",goods);

        //查询类型
        List<GoodsType> tlist = goodsService.findGoodsType();
        model.addAttribute("tlist",tlist);
        return "update";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(Integer id){
        goodsService.deleteGoods(id);
        return "redirect:findAll";
    }

    //添加
    @RequestMapping("/add")
    public String add(Goods goods){
        goodsService.addGoods(goods);
        return "redirect:findAll";
    }

    //去添加
    @RequestMapping("/toadd")
    public String toadd(Model model){
        List<GoodsType> tlist = goodsService.findGoodsType();
        model.addAttribute("tlist",tlist);
        return "add";
    }

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(defaultValue = "1") Integer pageNum, HttpServletRequest request){
        PageHelper.startPage(pageNum,5);
        List<GoodsVo> list = goodsService.findAll();
        PageInfo page = new PageInfo(list);
        request.setAttribute("page",page);
        return "goods";
    }


}
